/**
 *	数据选项
 *	开发者：雨中磐石
 *	地址：www.rockoa.com
 *	日期：2023-07-19
 */

package com.framework.Model;

import com.framerock.db.DBConn;
import com.framerock.baselib.CDate;
import com.framerock.baselib.CallBack;
import com.framerock.baselib.Rock;
import com.framework.ACore.Model;

import java.util.ArrayList;
import java.util.Map;

public class ModelOption extends Model {

    public ModelOption(DBConn dbs) {
        super(dbs);
        this.setTable("option");
    }

    public static ModelOption get(DBConn dbs)
    {
        return new ModelOption(dbs);
    }

    /**
     * 获取选项
     * */
    public String getVal(String num, String dev, String fid)
    {
        String str = dev;
        Map<String,String> rs = this.getOne("`num`='"+num+"'", "`value`,`name`,`id`,`optdt`");
        if(rs!=null){
            str = Rock.getMapString(rs, fid, dev);
        }
        return str;
    }
    public String getVal(String num, String dev)
    {
        return getVal(num,dev,"value");
    }
    public String getVal(String num)
    {
        return getVal(num, "", "value");
    }

    /**
     * 设置选项
     * */
    public void setVal(String num, String val)
    {
        setVal(num, Rock.getMap(new String[]{"value",val}));
    }

    public void setVal(String num, Map<String, String> da)
    {
        String where = "`num`='"+num+"'";
        int to = this.getCount(where);
        da.put("num", num);
        da.put("optdt", CDate.now());
        if(to==0){
            this.insert(da);
        }else{
            this.update(da, where);
        }
    }

    /**
     * 读取数据选项
     * */
    public ArrayList<Map<String, String>> getData(String num, final String fid)
    {
        String pid = "-1";
        Map<String, String> rs = this.getOne("`num`='"+num+"'");
        if(rs!=null)pid = rs.get("id");
        ArrayList<Map<String, String>> data = this.getAll("`pid`="+pid+" and `valid`=1", "`name`,`value`,`id`,`explain`", "`sort`",0, new CallBack(){
            public Map<String, String> backMap(Map<String, String> a) {
                Map<String, String> da = Rock.getMap();
                da.put("name", a.get("name"));
                da.put("value", a.get(fid));
                da.put("subname", a.get("explain"));
                return da;
            }
        });
        return data;
    }

    public ArrayList<Map<String, String>> getData(String num,String cid, String fid)
    {
        return getData(num+"_"+cid, fid);
    }

    /**
     * 读取所有的列表
     * */
    public ArrayList<Map<String, String>> getTreeData(String num,String cid, String fid)
    {
        ArrayList<Map<String, String>> rows = Rock.getMapList();
        String bh  = ""+num+"";
        if(!Rock.isEmpt(cid))bh+="_"+cid+"";
        Map<String, String> rs = this.getOne("`num`='"+bh+"'");
        if(rs==null)return rows;
        String pid = rs.get("id");
        getTreeDatas(pid, rows, fid);
        return rows;
    }
    private void getTreeDatas(String pid, ArrayList<Map<String, String>> data, String fid){
        ArrayList<Map<String, String>> rows = this.getAll("`pid` IN("+pid+") and `valid`=1", "`name`,`value`,`id`,`pid`", "`sort`");
        String pids = "";
        Map<String, String> da;
        if(rows!=null)for(int i=0;i<rows.size();i++){
            da = rows.get(i);
            pids+=","+da.get("id")+"";
            if(!Rock.isEmpt(fid)){
                da = Rock.getMap(new String[]{
                        "name",da.get("name"),
                        "value",da.get(fid)
                });
            }
            data.add(da);
        }
        if(!Rock.isEmpt(pids))getTreeDatas(pids.substring(1), data, fid);
    }

    public String allDownid(String pid)
    {
        String ids = pid;
        ArrayList<Map<String, String>> rows = Rock.getMapList();
        getTreeDatas(pid, rows, "");
        int len = rows.size(),i;
        for(i=0; i<len; i++){
            ids+=","+rows.get(i).get("id");
        }
        return ids;
    }
}
